package com.fc.happyteam.service.impl;

import com.fc.happyteam.dao.SysDictDao;
import com.fc.happyteam.entity.SysDictGroupEntity;
import com.fc.happyteam.entity.SysDictItemEntity;
import com.fc.happyteam.service.ISysDictService;
import com.fc.happyteam.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by futurecreative on 2017/7/25.
 */
@Service("sysDictService")
@Transactional
public class SysDictServiceImpl implements ISysDictService {

    @Autowired
    private SysDictDao sysDictDao;


    @Override
    public SysDictGroupEntity getSysDictGroup(Integer id) {
        return sysDictDao.getSysDictGroup(id);
    }

    @Override
    public SysDictGroupEntity getSysDictGroupByCode(String code) {
        return sysDictDao.getSysDictGroupByCode(code);
    }

    @Override
    public SysDictItemEntity getSysDictItem(Integer id) {
        return sysDictDao.getSysDictItem(id);
    }

    @Override
    public SysDictItemEntity getSysDictItemByCode(String code) {
        return sysDictDao.getSysDictItemByCode(code);
    }

    @Override
    public List<SysDictGroupEntity> querySysDictGroupList(Map<String, Object> map) {
        return sysDictDao.querySysDictGroupList(map);
    }

    @Override
    public int querySysDictGroupTotal(Map<String, Object> map) {
        return sysDictDao.querySysDictGroupTotal(map);
    }

    @Override
    public void saveDictItem(SysDictItemEntity sysDictItemEntity) {
        SysDictGroupEntity dictGroup = getSysDictGroup(sysDictItemEntity.getGroupId());
        if (dictGroup == null) {
            throw new RRException("group 不存在", 404);
        }
        SysDictItemEntity exist = getSysDictItemByCode(sysDictItemEntity.getCode());
        if (exist != null) {
            throw new RRException("code 已存在", 409);
        }
        sysDictDao.saveSysDictItem(sysDictItemEntity);
    }

    @Override
    public void saveDictGroup(SysDictGroupEntity sysDictGroupEntity) {
        SysDictGroupEntity exist = getSysDictGroupByCode(sysDictGroupEntity.getCode());
        if (exist != null) {
            throw new RRException("code 已存在", 409);
        }
        sysDictDao.saveSysDictGroup(sysDictGroupEntity);
    }

    @Override
    public void updateSysDictItem(SysDictItemEntity sysDictItemEntity) {
        SysDictGroupEntity dictGroup = getSysDictGroup(sysDictItemEntity.getGroupId());
        if (dictGroup == null) {
            throw new RRException("group 不存在", 404);
        }
        SysDictItemEntity exist = getSysDictItemByCode(sysDictItemEntity.getCode());
        if (exist == null) {
            throw new RRException("code 不存在", 409);
        }
        sysDictItemEntity.setId(exist.getId());
        sysDictDao.updateSysDictItem(sysDictItemEntity);
    }

    @Override
    public void updateSysDictGroup(SysDictGroupEntity sysDictGroupEntity) {
        SysDictGroupEntity exist = getSysDictGroupByCode(sysDictGroupEntity.getCode());
        if (exist == null) {
            throw new RRException("code 不存在", 409);
        }
        sysDictGroupEntity.setId(exist.getId());
        sysDictDao.updateSysDictGroup(sysDictGroupEntity);
    }

    @Override
    public void deleteSysDictItems(Integer[] ids) {
        sysDictDao.deleteSysDictItems(ids);
    }

    @Override
    public void deleteSysDictGroups(Integer[] ids) {
        sysDictDao.deleteSysDictGroups(ids);
    }
}
